/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
    
package com.googlecode.jsimplemarket.wicket.pages;
    
import com.googlecode.jsimplemarket.model.Creanta;
import com.googlecode.jsimplemarket.model.Inchiriere;
import com.googlecode.jsimplemarket.model.Plata;
import com.googlecode.jsimplemarket.model.Absenta;
import com.googlecode.jsimplemarket.model.TotalIncasari;
import com.googlecode.jsimplemarket.service.GenericService;
import com.googlecode.jsimplemarket.service.InchiriereService;
import com.googlecode.jsimplemarket.service.AbsentaService;
import com.googlecode.jsimplemarket.wicket.pages.RaportIncasariZilnicePage.TotalIncasariView;

import java.util.Date;
//import java.util.Calendar;
//import java.util.GregorianCalendar;
import java.util.Locale;
//import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.NumberFormat;
    
import java.util.List;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
//import org.apache.wicket.behavior.AttributeAppender;

/**
 *
 * @author Alexandru.Codescu
 */
public class RaportAbsentePage extends DefaultPageTemplate {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @SpringBean(name = "genericService")
    private GenericService<Inchiriere> serviceInchiriere;
    @SpringBean(name = "genericService")
    private GenericService<Creanta> serviceCreanta;
    @SpringBean(name = "genericService")
    private GenericService<Plata> servicePlata;
    @SpringBean(name = "inchiriereService")
    private InchiriereService inchiriereService;
    @SpringBean(name = "genericService")
    private GenericService<Absenta> serviceAbsenta;
    @SpringBean(name = "absentaService")
    private AbsentaService absentaService;

    private Date dataRaport = new Date();
    private Locale locale = new Locale("ro","RO");
    private NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
    
    public RaportAbsentePage() {

        class AbsenteView extends ListView<Object[]> {

            public AbsenteView(String id, List<Object[]> data) {
                super(id, data);
            }

            @Override
            protected void populateItem(ListItem<Object[]> li) {
                final Object[] object = li.getModelObject();
                int index;
                if (object==null){
                    index = 0;
                } else {
                    index = li.getIndex() + 1;
                }
                
                li.add(new Label("index", object==null ? "-" : index));
                //li.add(new Label("taraba", object==null ? "-" : object[0].toString()));
                
                Link lnkTaraba = new Link("lnkTaraba") {
                    @Override
                    public void onClick() {
                        setResponsePage(new TarabaViewPage((Long) object[2]));
                    }
                };
                
                lnkTaraba.add(new Label("taraba", object==null ? "-" : object[0].toString()));
                li.add(lnkTaraba);
                
                li.add(new Label("ocupant", object==null ? "-" : object[1].toString()));
                
                if (object[0]!=null){
                    Link delLink = new Link("delLink") {
                        @Override
                        public void onClick() {

                            Absenta absenta = serviceAbsenta.getById(Absenta.class, Long.valueOf(object[4].toString()));
                            serviceAbsenta.delete(absenta);

                            setResponsePage(new RaportAbsentePage());
                        }
                    };
                    li.add(delLink);
                } else {
                    Link delLink = new Link("delLink") {
                        @Override
                        public void onClick() {
                            setResponsePage(new RaportAbsentePage());
                        }
                    };
                    li.add(delLink);
                }
                
            }
        }

        final Form<RaportAbsentePage> form = new Form<RaportAbsentePage>("form");
        form.setModel(new CompoundPropertyModel<RaportAbsentePage>(this));
        
        final DateTextField tfDataRaport = new DateTextField("dataRaport", new PropertyModel<Date>(this, "dataRaport"), "dd.MM.yyyy");
        tfDataRaport.add(new DatePicker());
        form.add(tfDataRaport);

        final AbsenteView raportView = new AbsenteView("raport", absentaService.queryByDayAll(dataRaport));

        final WebMarkupContainer wkRaport = new WebMarkupContainer("wkRaport");
        wkRaport.setOutputMarkupId(true);
        wkRaport.setOutputMarkupPlaceholderTag(true);
        wkRaport.add(raportView);
        
        form.add(wkRaport);
        form.add(new AjaxSubmitLink("submit") {

            @Override
            public void onSubmit(AjaxRequestTarget target, Form form) {
                super.onSubmit(target, form);
                tfDataRaport.processInput();
                raportView.setList(absentaService.queryByDayAll(tfDataRaport.getConvertedInput()));
                raportView.modelChanged();
                
                target.add(wkRaport);
            }
            
        });
        add(form);

    }
}